package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.reggie.bean.Category;
import com.zrrd.reggie.bean.Dish;
import com.zrrd.reggie.bean.Setmeal;
import com.zrrd.reggie.bean.SetmealDish;
import com.zrrd.reggie.bean.dto.SetmealDto;
import com.zrrd.reggie.service.CategoryService;
import com.zrrd.reggie.service.SetmealDishService;
import com.zrrd.reggie.service.SetmealService;
import com.zrrd.reggie.util.CustException;
import com.zrrd.reggie.util.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        Page<Setmeal> setmealPage=new Page<>(page,pageSize);
        QueryWrapper<Setmeal> wrapper=new QueryWrapper<>();
        if(name!=null&&!name.equals("")){
            wrapper.like("name",name);
        }
        setmealService.page(setmealPage,wrapper);
        Page<SetmealDto> setmealDtoPage=new Page<>();
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> list=new ArrayList<>();
        for (Setmeal record : records) {
            SetmealDto dto=new SetmealDto();
            BeanUtils.copyProperties(record,dto);
            Category category=categoryService.getById(record.getCategoryId());
            if(category!=null){
                dto.setCategoryName(category.getName());
            }
            list.add(dto);
        }
        setmealDtoPage.setRecords(list);
        return R.sucess(setmealDtoPage);
    }
    @PostMapping
    @CacheEvict(value="setmealCache",allEntries = true)
    public R<String> saveSetmealWithDish(@RequestBody SetmealDto dto){
        setmealService.saveSetmealWithDish(dto);
        return R.sucess("新增成功");
    }
    @GetMapping("/{id}")
    public R<SetmealDto> getId(@PathVariable("id") Long id){
        return R.sucess(setmealService.getSetmealWithDish(id));
    }
    @PutMapping
    @CacheEvict(value="setmealCache",allEntries = true)
    public R<String> updateSetmealWithDish(@RequestBody SetmealDto dto){
        return R.sucess(setmealService.updateSetmealWithDish(dto)?"修改成功":"修改失败");
    }
    @DeleteMapping
    @CacheEvict(value="setmealCache",allEntries = true)
    public R<String> deleteSetmealWithDish(Long[] ids) throws CustException {
        setmealService.deleteSetmealWithDish(ids);
        return R.sucess("删除成功");
    }
    @PostMapping("/status/{status}")
    @CacheEvict(value="setmealCache",allEntries = true)
    public R<String> updateStatus(@PathVariable("status") Integer status,Long[] ids){
        for(Long id:ids){
            Setmeal setmeal=new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.sucess("修改成功");
    }
    @Cacheable(value="setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public R<List> list(Setmeal setmeal){
        QueryWrapper<Setmeal> wrapper=new QueryWrapper<>();
        wrapper.eq("category_id",setmeal.getCategoryId());
        wrapper.eq("status",setmeal.getStatus());
        List<Setmeal> list = setmealService.list(wrapper);
        return R.sucess(list);
    }
    @GetMapping("/dish/{id}")
    public R<List> getDishListById(@PathVariable("id") Long id){
        QueryWrapper<SetmealDish> wrapper=new QueryWrapper<>();
        wrapper.eq("setmeal_id",id);
        List<SetmealDish> list = setmealDishService.list(wrapper);
        return R.sucess(list);
    }
}
